package com.wva.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * @Author: hong
 * @Date: 2025/2/27 13:13:02
 */
@Data
public class JsonUtils {
    @Autowired
    ObjectMapper objectMapper;

    public <R> R parse(String json, Class<R> type) throws JsonProcessingException {
        return objectMapper.readValue(json, type);
    }

    public String toJson(Object object) throws JsonProcessingException {
        return objectMapper.writeValueAsString(object);
    }

    /**
     * Map转实体
     * @param map
     * @param type
     * @return
     * @param <K>
     * @param <V>
     * @param <R>
     * @throws JsonProcessingException
     */
    public <K, V, R> R convertMap(Map<K, V> map, Class<R> type) throws JsonProcessingException {
        return parse(toJson(map), type);
    }

}
